<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Db
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: Form.php 17 2009-11-15 01:58:48Z nkeyes $
 */

/**
 * 
 * @author Nathan Keyes
 *
 */
class Bushido_Db_Form extends Bushido_Form
{
	protected $_row;
	public function __construct(Zend_Db_Table_Row_Abstract  $row, $options = array())
	{
		if(!($row->getTable() instanceof Bushido_Db_Table_Abstract))
		{
			throw new Exception('$row passed to the constructor for Bushido_Db_Form must be from an instance of Bushido_Db_Table_Abstract');
		}
		
		parent::__construct($options);
		$this->_row = $row;
		//echo __FILE__ . ' line ' . __LINE__ . "<br />\n";
		
		$this->_setForm();
		
		//$this->addElement('hash', 'heisenberg_compensator', array('salt' => md5(uniqid(mt_rand(), true))));
		//$this->addElement('hash', 'heisenberg_compensator', array('salt' => 'cochran'));
		
	}

	
	/**
	 * 
	 * @return Bushido_Db_Table_Row
	 */
	public function getRow()
	{
		return $this->_row;
	}
	/**
	 * 
	 * @param $configOptions
	 * @param $update
	 * @return void
	 */
	protected function _setForm()
	{
		$this->setEnctype(self::ENCTYPE_MULTIPART);
		$this->_setRegularFields();
		$this->_setPKs();
		
		$this->_setReferences();
		
		//$this->_clearHiddenLabels($this);
		//$this->_setSubForms();
	}
	
	
	
	/**
	 * 
	 * @return unknown_type
	 */
	protected function _setPKs()
	{
		foreach($this->getRow()->getTable()->info('metadata') as $field => $fieldData)
		{
			
			if(!empty($fieldData['PRIMARY']) && !empty($fieldData['IDENTITY']))
			{
				/**
				 * this table has a sequenced (auto-increment) primark key, hide the pk field
				 */
				
				$element = $this->createElement('hidden', $field);

				$this->addElement($element);
				
			}
		}
		unset($field);
		unset($filedData);
	}
	
	
	/**
	 * 
	 * @return void
	 */
	protected function _setRegularFields()
	{
		foreach($this->getRow()->getTable()->info('metadata') as $field => $fieldData)
		{
			$this->generateElement($field, $fieldData);
		}
		unset($field);
		unset($fieldData);
	}
	
	/**
	 * 
	 * @return void
	 */
	protected function _setReferences()
	{
		$references = array();
		$defaults = array();
		foreach($this->getRow()->getTable()->info('metadata') as $field => $fieldData)
		{
			//Zend_Debug::dump($field, '$field');
			foreach($this->getRow()->getTable()->getReferenceMap() as $reference => $referenceData)
			{
				//echo __FILE__ . ' line ' . __LINE__ . "<br />\n";
				if(is_array($referenceData['columns']) && in_array($field, $referenceData['columns']))
				{
					 // @todo implement multi-column FK handling
					
					continue;
				}
				elseif($referenceData['columns'] == $field)
				{
					//Zend_Debug::dump($referenceData, '$referenceData');
					$fieldType = 'FilteringSelect';
					$fieldOptions = array('label' => $field);
					$fieldValue = '';
					$multiOptions = array();
					
					//echo __FILE__ . ' line ' . __LINE__ . "<br />\n";
					if(isset($fieldData['fieldDetails']) && is_array($fieldData['fieldDetails']))
					{
						$fieldType = isset($fieldData['fieldDetails']['type']) ? $fieldData['fieldDetails']['type'] : $fieldType;
						if($fieldType == 'generated')
						{
							continue;
						}
						if($fieldType == 'subForm')
						{
							//$this->addSubForm(Bushido_Db_Table::getTable($referenceData['refTableClass'])->getSubForm(), $reference . '_subForm');
							continue;
						}
						$fieldOptions = isset($fieldData['fieldDetails']['options']) ? $fieldData['fieldDetails']['options'] : $fieldOptions;
						$fieldValue = isset($fieldData['fieldDetails']['defaultValue']) ? $fieldData['fieldDetails']['defaultValue'] : $fieldValue;
					}
					
					$defaults[$field] = $fieldValue;
					$adapter = Bushido_Db_Table::getAdapter();
					
					$referenceTable = Bushido_Db_Table::getTable($referenceData['refTableClass']);
					
					
					if(strtolower($fieldType) != 'hidden')
					{
						$multiOptions[''] = '';

						//Zend_Debug::dump($referenceTable->getName());
						$referenceRows = $referenceTable->fetchAll();

						foreach($referenceRows as $referenceRow)
						{
							$multiOptions[$referenceRow->$referenceData['refColumns']] = Bushido_Db_Table::getDisplayValue($referenceRow);
						}
						//echo __FILE__ . ' line ' . __LINE__ . "<br />\n";
						unset($referenceRow);
						
						//var_dump($multiOptions);
						
						$fieldOptions['multiOptions'] = $multiOptions;
					}
					//echo __FILE__ . ' line ' . __LINE__ . "<br />\n";
					$element = $this->createElement($fieldType, $field, $fieldOptions);
					if(false && Bushido_DbTable_User::hasRole(Bushido_App::session()->userInfo['bushido_user_id'], Bushido_DbTable_Role::ADMIN))
					{
						$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
						//$parentUrl = Zend_Controller_Front::getInstance()->getRequest()->
						$createRefrenceRecordHTML = <<<EOS

<div dojoType="dijit.form.Button">
			<span>Add New ${fieldOptions['label']}</span>
			<script type="dojo/connect" event="onClick">
			var config = {title: 'New ${fieldOptions['label']}', href: '${baseUrl}/Admin/records/add/table/${referenceData['refTableClass']}/useLayout/false'};
			if(dojo.query('.dijitDialog').length > 0)
			{
				var parentConfig = {title: '', href: '<?= ${baseUrl}/Task/Task/view/useLayout/false/task_id/<?= $this->task->task_id ?>'};
				newModalDialog(config, parentConfig);
			}
			else
			{
				newModalDialog(config);
			}
		</script>
		</div>
EOS;
						$element->setDescription($element->getDescription() . $createRefrenceRecordHTML);
					}
					//echo __FILE__ . ' line ' . __LINE__ . "<br />\n";
					$element->getDecorator('description')->setEscape(false);
					$this->addElement($element);
					
					
				}
			}
			unset($reference);
			unset($referenceData);
		}
		unset($field);
		unset($fieldData);
		$this->populate($defaults);
	}
	/**
	 * 
	 * @return void
	 */
	protected function _setSubForms()
	{
		foreach($this->getRow()->getTable()->getChildMap() as $childName => $childRelationshipInfo)
		{
			$childClass = Bushido_Db_Table::getTable($childRelationshipInfo['refTableClass'])->fetchNew();
			
			if(in_array($childRelationshipInfo['cardinality'], array(Bushido_Db_Table_Abstract::ZERO_OR_ONE, Bushido_Db_Table_Abstract::ONE), true))
			{
				$subForm = new Bushido_Db_Form_SubForm($childClass);
			}
			elseif(in_array($childRelationshipInfo['cardinality'], array(Bushido_Db_Table_Abstract::ZERO_OR_MANY, Bushido_Db_Table_Abstract::MANY, Bushido_Db_Table_Abstract::ZERO_OR_ONE), true))
			{
				$subForm = new Zend_Form_SubForm();
				
				$fieldOptions = array('label' => $childName);
				$multiOptions = array();
				$fieldType = 'MultiSelect';
				
				
				$referenceTable = new $childRelationshipInfo['refTableClass']($adapter);
				
				$multiOptions[''] = '';
				$rows = $referenceTable->fetchAll();
				foreach($rows as $referenceRow)
				{
					$PKField = current($referenceTable->info(Bushido_Db_Table_Abstract::PRIMARY));
					
					$multiOptions[$referenceRow->$PKField] = $referenceTable->getDisplayValue($referenceRow);
				}
				unset($referenceRow);
				
				$fieldOptions['multiOptions'] = $multiOptions;
				
				$element = $this->createElement($fieldType, $childName, $fieldOptions);

				$subForm->addElement($element);
				
			}
			
			$this->addSubForm($subForm, $childName);
		}
		unset($childName);
		unset($childRelationshipInfo);
	}
	
	/**
	 * 
	 * @param $childName
	 * @param $childTableClass
	 * @param $childMiddlemanTableClass
	 * @return Zend_Dojo_Form_SubForm
	 */
	public function getChildManyToManySubForm($childName, $childTableClass, $childMiddlemanTableClass, Zend_Db_Table_Rowset_Abstract $allowedRowset, $fieldType)
	{
		$subForm = new Zend_Dojo_Form_SubForm();
				
		$fieldOptions = array
		(
			'label' => $childName,
		);
		
		if(strtoupper($fieldType) === 'FILTERINGSELECT')
		{
			$multiOptions[0] = ' ';
		}
		$referenceTable = $allowedRowset->getTable();
		
		foreach($allowedRowset as $referenceRow)
		{
			$PKField = current($referenceTable->info(Bushido_Db_Table_Abstract::PRIMARY));
			
			$multiOptions[(string)$referenceRow->$PKField] = $referenceTable->getDisplayValue($referenceRow);
		}
		unset($referenceRow);
		$fieldOptions['multiOptions'] = $multiOptions;
		//$fieldOptions['multiOptions'] = array('1' => 'one');
		
		$element = $subForm->createElement($fieldType, $childName, $fieldOptions);
		$subForm->addElement($element);
		
		$subForm->setAttribs
		(
			array
			(
				'name'   => $childName . '_Pane',
				'legend' => $childName,
				'dijitParams' => array
				(
					'title' => $childName
				)
			)
		);
		
		return $subForm;
	}
	
	
	
	/**
	 * 
	 * @param $childName
	 * @param $childTableClass
	 * @param $childMiddlemanTableClass
	 * @return Zend_Dojo_Form_SubForm
	 */
	public function getChildOneToOneSubForm($childName, $childTableClass)
	{
		
		$parentTable = $this->getRow()->getTable();
		//echo __CLASS__ . '::' . __FUNCTION__ . '() line ' . __LINE__ . '<br />';exit;
		
		$rowset = $this->_findChildren($childTableClass);
		
		$childTable = $rowset->getTable();
		
		$subForm = $childTable->getSubForm();
		//echo  get_class($this) . ' ' . __FILE__ . ' line: ' . __LINE__ . "<br />\n";
		$subForm->removeElement(current($parentTable->info(Zend_Db_Table_Abstract::PRIMARY)));
		
		$subForm->setAttribs
		(
			array
			(
				'name' => $childName . '_Pane',
				'legend' => $childName,
				'style' => 'width: 30em;',
				'dijitParams' => array
				(
					'title' => $childName,
					'open' => false,
					'duration' => 300
				)
			)
		);
		
		$subForm->setDecorators(array('PrepareElements', 'FormElements'));
		
		return $subForm;
	}
	
	
	
	/**
	 * 
	 * @param $children
	 * @param $row
	 * @return void
	 */
	public function addManyToManySubForm($childName, $childTableClass, $childMiddlemanTableClass, Zend_Db_Table_Rowset_Abstract $allowedRowset = null, $fieldType = 'Multiselect')
	{

		$childName = $this->filterName($childName);
		$findFunction = 'find' . $childTableClass . 'Via' . $childMiddlemanTableClass;
		
		$rowset = $this->getRow()->$findFunction();
		//echo __CLASS__ . '::' . __FUNCTION__ . '() line ' . __LINE__ . '<br />';exit;
		if(!$allowedRowset)
		{
			$allowedRowset = $rowset->getTable()->fetchAll();
		}
		//$childForm = $this->getChildManyToManySubForm($childName, $allowedRowset);
		$childForm = $this->getChildManyToManySubForm($childName, $childTableClass, $childMiddlemanTableClass, $allowedRowset, $fieldType);
		 
		$this->addSubForm($childForm, $childName . '_subForm');
		//var_dump($this->getRow());
		$rowData = $this->getRow()->toArray();
		foreach($rowData as $key => $value)
		{
			if(empty($rowData[$key]))
			{
				unset($rowdata[$key]);
			}
		}
		if(!empty($rowset))
		{
			$rowData[$childName] = array();
		
			foreach($rowset as $selectedChild)
			{
				$PK = current($selectedChild->getTable()->info(Zend_Db_Table::PRIMARY));
				$rowData[$childName . '_subForm'][$childName][] = $selectedChild->$PK;
			}
			unset($selectedChild);
		}
		$this->populate($rowData);
		
	}
	
	
	/**
	 * 
	 * @param $children
	 * @param $defaultValues
	 * @return void
	 */
	public function addManyToManyChildren($children, $defaultValues = array())
	{
		$parentTable = $this->getRow()->getTable();
		$parentPK = current($parentTable->info(Zend_Db_Table_Abstract::PRIMARY));
		
		
		$parentId = isset($defaultValues[$parentPK]) ? $defaultValues[$parentPK] : $this->getRow()->$parentPK;

		foreach($children as $childFormLabel => $childClassData)
		{
			$childFormLabel = $this->filterName($childFormLabel);

			
			$childValues = current($this->getValue($childFormLabel . '_subForm'));
			//var_dump($childValues);
			
			if($childValues)
			{
				$childValues = is_array($childValues) ? $childValues : array($childValues);
			}
			if(count($childValues))
			{

				//exit;
				$childMiddleManTable = Bushido_Db_Table::getTable($childClassData['childClassMiddleMan']);
				$childTable = Bushido_Db_Table::getTable($childClassData['childClassName']);
				$childPK = current($childTable->info(Zend_Db_Table_Abstract::PRIMARY));
				if(is_array($childValues))
				{
					foreach($childValues as $key => $childValue)
					{
						while(is_array($childValue))
						{
							$childValue = current($childValue); // hack to deal with dojo editor bug
						}
						
						$insertValues = array_merge
						(
							array
							(
								$parentPK => $parentId,
								$childPK => $childValue,
							),
							$defaultValues
							
						);
						$this->insert
						(
							$childMiddleManTable, $insertValues
						);
						//echo __CLASS__ . '::' . __FUNCTION__ . '() line ' . __LINE__ . '<br />';exit;
	
					}
					unset($key);
					unset($value);
				}
			}
		}
		unset($childFormLabel);
		unset($childClassData);
	}
	
	
	
	public function filterExistingManyToManyChildren($children)
	{
		$parentTable = $this->getRow()->getTable();
		$parentPK = current($parentTable->info(Zend_Db_Table_Abstract::PRIMARY));
		
		
		$parentId = $this->getRow()->$parentPK;
		if(!$parentId)
		{
			return;
		}

		foreach($children as $childFormLabel => $childClassData)
		{
			$childFormLabel = $this->filterName($childFormLabel);
			$childValues = current($this->getValue($childFormLabel . '_subForm'));			
			
			if(!empty($childValues) && !is_array($childValues))
			{
				$childValues = array($childValues);
			}
			if(is_array($childValues))
			{
				//exit;
				$childMiddleManTable = Bushido_Db_Table::getTable($childClassData['childClassMiddleMan']);
				$childTable = Bushido_Db_Table::getTable($childClassData['childClassName']);
				//var_dump($childValues);
				foreach($childValues as $key => $childValue)
				{
					while(is_array($childValue))
					{
						$childValue = current($childValue); // hack to deal weith dojo editor bug
					}
					
					$existingRows = $childMiddleManTable->fetchAll
					(
						$childMiddleManTable->select()
						->where($parentPK . ' = ?',  $parentId)
						->where
						(
							current($childTable->info(Zend_Db_Table_Abstract::PRIMARY)) . ' = ?',  $childValue
						)
					);
					
					if($existingRows->count())
					{

						$value = $this->getSubForm($childFormLabel . '_subForm')->getElement($childFormLabel)->getValue();
						if(is_array($value))
						{
							unset($value[array_search($childValue, $value)]);
						}
						else
						{
							$value = null;
						}
						//var_dump($value);exit;
						$this->getSubForm($childFormLabel . '_subForm')->getElement($childFormLabel)->setValue($value);
						$value = $this->getSubForm($childFormLabel . '_subForm')->getElement($childFormLabel)->getValue();
						
					}
				}
				unset($key);
				unset($childValue);
			}
		}
		unset($childFormLabel);
		unset($childClassData);
	}
	
	/**
	 * 
	 * @param $children
	 * @return void
	 */
	public function deleteManyToManyChildren($children, $whereValues = array())
	{		
		
		foreach($children as $childFormLabel => $childClassData)
		{
			$parentPK = current($this->getRow()->getTable()->info(Zend_Db_Table_Abstract::PRIMARY));
			$this->delete
			(
				$childClassData['childClassMiddleMan'],
				array_merge
				(
					$whereValues,
					array
					(
						$parentPK => $this->getRow()->$parentPK
					)
				)
			);
		}
		unset($childFormLabel);
		unset($childClassData);
	}
	
	
	/**
	 * 
	 * @param $children
	 * @param $defaultValues
	 * @return void
	 */
	public function updateManyToManyChildren($children, $defaultValues = array())
	{
		$this->deleteManyToManyChildren($this->getRow(), $children, $defaultValues);
		$this->addManyToManyChildren($this, $this->getRow(), $children, $defaultValues);
	}
	
	
	/**
	 * 
	 * @param $this->getRow()
	 * @param $children
	 * @param $defaultValues
	 * @return void
	 */
	public function updateOneToOneChildren($children, $defaultValues = array())
	{
		$parentTable = $this->getRow()->getTable();
		$parentPK = current($parentTable->info(Zend_Db_Table_Abstract::PRIMARY));
		
		$parentId = $this->getRow()->$parentPK;
		
		foreach($children as $childFormLabel => $childClassData)
		{
			$childFormLabel = $this->filterName($childFormLabel);

			
			$childValues = $this->getValue($childFormLabel . '_subForm');

			//var_dump($childValues);exit;
			$childTable = new $childClassData['childClassName']($parentTable->getAdapter());
			
			$this->update
			(
				$childTable, array_merge
				(
					$defaultValues,
					$childValues
				),
				array($parentPK => $parentId)
			);
		}
		unset($childFormLabel);
		unset($childClassData);
	}
	
	/**
	 * 
	 * @param $this->getRow()
	 * @param $children
	 * @param $defaultValues
	 * @return void
	 */
	public function addOneToOneChildren($children, $defaultValues = array())
	{
		$parentTable = $this->getRow()->getTable();
		$parentPK = current($parentTable->info(Zend_Db_Table_Abstract::PRIMARY));
		
		
		$parentId = $this->getRow()->$parentPK;
		foreach($children as $childFormLabel => $childClassData)
		{
			$childFormLabel = $this->filterName($childFormLabel);

			
			$childValues = $this->getValue($childFormLabel . '_subForm');
			
			foreach($childValues as $key => $value)
			{
				while(is_array($childValues[$key]))
				{
					$childValues[$key] = current($childValues[$key]); // hack to deal weith dojo editor bug
				}
				$element = $this->getSubForm($childFormLabel . '_subForm')->getElement($key);
				if($element instanceof Zend_Form_Element_File)
				{
					if(!$element->getTransferAdapter()->isUploaded())
					{
						return;
					}
					$element->receive();
					$fileInfo = $element->getTransferAdapter()->getFileInfo();
					$fileInfo = $fileInfo[$key];
					//var_dump($fileInfo);exit;
					$childValues['filename'] = $fileInfo['name'];
					$childValues['size'] = $fileInfo['size'];
					
					$childValues['mime_type'] = $fileInfo['type'];
					$fileName = $element->getFileName();
					$fileHandle = fopen($fileName, 'r');
					
					$childValues['content'] = fread($fileHandle, $childValues['size']);
				}
			}
			unset($key);
			unset($value);

			//var_dump($childValues);exit;
			$childTable = new $childClassData['childClassName']($parentTable->getAdapter());
			
			$insertValues = array_merge
				(
					array($parentPK => $parentId),
					$defaultValues,
					$childValues
				);
			//var_dump($insertValues);exit;
			$this->insert($childTable, $insertValues);
		}
		unset($childFormLabel);
		unset($childClassData);
	}
	
	
	/**
	 * 
	 * @param $childName
	 * @param $childTableClass
	 * @param $childMiddlemanTableClass
	 * @return void
	 */
	public function addOneToOneSubForm($childName, $childTableClass, $order = null)
	{
		$childName = $this->filterName($childName);
		$rowset = $this->_findChildren($childTableClass);
		
		$PK = current($this->getRow()->getTable()->info(Zend_Db_Table_Abstract::PRIMARY));
		
		$childForm = $this->getChildOneToOneSubForm($childName, $childTableClass);
		
		$this->addSubForm($childForm, $childName . '_subForm', $order);
		
		if($this->getRow() && $this->getRow()->$PK && $rowset && count($rowset))
		{
			$childTable = $rowset->getTable();
			//var_dump($selectedChild->toArray());exit;
			$rowData = $this->getRow()->toArray();
			
			$rowData[$childName . '_subForm'] = array();
			
			$rowData[$childName . '_subForm'] = $rowset->current()->toArray();
			
			$this->populate($rowData);
		}
	}
	
	
	/**
	 * 
	 * @param $table
	 * @param $data
	 * @return array
	 */
	protected function _filterUnknownFields($table, $data)
	{
		if(!($table instanceof Zend_Db_Table_Abstract))
		{
			$table = Bushido_Db_Table::getTable($table);
		}
		$row = $table->fetchNew()->toArray();

		return array_intersect_key($data, $row);
	}
	
	/**
	 * 
	 * @param $table
	 * @return Zend_Db_Table_Rowset_Abstract
	 */
	protected function _list($table)
	{
		if(!($table instanceof Zend_Db_Table_Abstract))
		{
			$table = Bushido_Db_Table::getTable($table);
		}
		return $table->fetchAll();
	}
	
	
	/**
	 * 
	 * @param $table
	 * @param $data
	 * @param $whereData
	 * @return void
	 */
	public function update($table, $data, $whereData)
	{
		if(!($table instanceof Zend_Db_Table_Abstract))
		{
			$table = Bushido_Db_Table::getTable($table);
		}
		$filteredData = $this->_filterUnknownFields($table, $data);
		$where = $this->_buildWhere($table, $whereData);
		$table->update($filteredData, $where);
		
	}
	/**
	 * 
	 * @param $table
	 * @param $data
	 * @return unknown_type
	 */
	public function insert($table, $data)
	{
		if(!($table instanceof Zend_Db_Table_Abstract))
		{
			$table = Bushido_Db_Table::getTable($table);
		}
		$filteredData = $this->_filterUnknownFields($table, $data);
		try
		{
			return $table->insert($filteredData);
		}
		catch(Exception $e)
		{
			var_dump("tried to insert");
			var_dump($e->getMessage());
			var_dump($data);exit;
			
		}
	}
	
	
	/**
	 * 
	 * @param $table
	 * @param $whereData
	 * @return int
	 */
	public function delete($table, $whereData)
	{
		if(!($table instanceof Zend_Db_Table_Abstract))
		{
			$table = Bushido_Db_Table::getTable($table);
		}
		$where = $this->_buildWhere($table, $whereData);
		//var_dump($where);exit();	
		return $table->delete($where);
	}
	
	
	
	/**
	 * 
	 * @param $data
	 * @return array
	 */
	protected function _buildWhere($table, $whereData, $PKsOnly = false)
	{
		$exprs = array();
		foreach($whereData as $field => $value)
		{
		if($value instanceof Zend_Db_Expr)
			{
				$exprs[] = $value;
			}
		}
		unset($field);
		unset($value);
		
		if(!($table instanceof Zend_Db_Table_Abstract))
		{
			$table = Bushido_Db_Table::getTable($table);
		}
		
		if($PKsOnly)
		{
			$whereData = array_intersect_key($whereData, array_flip($table->info(Zend_Db_Table::PRIMARY)));
		}
		else
		{
			$row = $table->fetchNew()->toArray();
			$whereData = array_intersect_key($whereData, $row);
		}
		$whereData = array_merge($whereData, $exprs);
		$where = array();
		foreach($whereData as $field => $value)
		{
			if($value instanceof Zend_Db_Expr)
			{
				$where[] = $value->__toString();
			}
			elseif(is_array($value))
			{
				$quote = create_function('&$value,$k,$table', 'return $table->getAdapter()->quoteInto("?", $value);');
				
				$value = array_map($quote, $value, $table);
				$values = implode(',', $value);
				
				$where[] = "$field IN ($values)";
			}
			elseif(trim($value) !== '')
			{
				$where[] = $table->getAdapter()->quoteInto("$field = ?", $value);
			}
			else
			{
				$where[] = "$field IS NULL";
			}
		}
		unset($field);
		unset($value);
		
		$where = implode(" AND ", $where);
		return $where;
	}
	
	protected function _findChildren($childTableClass)
	{
		$childTableClassObject = Bushido_Db_Table::getTable($childTableClass);
		
		$PK = current($this->getRow()->getTable()->info(Zend_Db_Table_Abstract::PRIMARY));
		if($this->getRow()->$PK !== null)
		{
			//echo __CLASS__ . '::' . __FUNCTION__ . '() line ' . __LINE__ . '<br />';exit;
			$findFunction = 'find' . $childTableClass;
			$rowset = $this->getRow()->$findFunction();
		}
		else
		{
			$rowset = $childTableClassObject->fetchNew();
		}
		
		return $rowset;
	}
}